In [ ]:
#Requests Module
#The requests library is the de facto standard for making HTTP requests in Python. It abstracts the complexities of making requests behind a beautiful, simple API so that you can focus on interacting with services and consuming data in your application.
In [2]:
import requests
In [3]:
r = requests.get('https://xkcd.com/353/')
print(r) #will give the status code of response
<Response [200]>
In [4]:
#to know about attributes and method that we can access within response object
print(dir(r))
['__attrs__', '__bool__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__nonzero__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_content', '_content_consumed', '_next', 'apparent_encoding', 'close', 'connection', 'content', 'cookies', 'elapsed', 'encoding', 'headers', 'history', 'is_permanent_redirect', 'is_redirect', 'iter_content', 'iter_lines', 'json', 'links', 'next', 'ok', 'raise_for_status', 'raw', 'reason', 'request', 'status_code', 'text', 'url']
In [5]:
#to get information about each attribute and method we can use help
#print(help(r))
In [6]:
#It will give content of the response in unicode
print(r.text)
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="/s/b0dcca.css" title="Default"/>
<title>xkcd: Python</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<link rel="shortcut icon" href="/s/919f27.ico" type="image/x-icon"/>
<link rel="icon" href="/s/919f27.ico" type="image/x-icon"/>
<link rel="alternate" type="application/atom+xml" title="Atom 1.0" href="/atom.xml"/>
<link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="/rss.xml"/>
<script type="text/javascript" src="/s/b66ed7.js" async></script>
<script type="text/javascript" src="/s/1b9456.js" async></script>

<meta property="og:site_name" content="xkcd">

<meta property="og:title" content="Python">
<meta property="og:url" content="https://xkcd.com/353/">
<meta property="og:image" content="https://imgs.xkcd.com/comics/">
<meta name="twitter:card" content="summary_large_image">

</head>
<body>
<div id="topContainer">
<div id="topLeft">
<ul>
<li><a href="/archive">Archive</a></li>
<li><a href="http://what-if.xkcd.com">What If?</a></li>
<li><a href="http://blag.xkcd.com">Blag</a></li>
<li><a href="/how-to/">How To</a></li>
<li><a href="http://store.xkcd.com/">Store</a></li>
<li><a rel="author" href="/about">About</a></li>
<li><a href="/atom.xml">Feed</a> &bull; <a href="/newsletter/">Email</a></li>
</ul>
</div>
<div id="topRight">
<div id="masthead">
<span><a href="/"><img src="/s/0b7742.png" alt="xkcd.com logo" height="83" width="185"/></a></span>
<span id="slogan">A webcomic of romance,<br/> sarcasm, math, and language.</span>
</div>
<div id="news">
<div id="htNews">
Going to San Diego Comic Con this week? I'll <a href="http://blog.xkcd.com/?p=894">see you there</a>!
</div>
<script>
var client = new XMLHttpRequest();
client.open("GET", "//c.xkcd.com/how-to/news", true);
client.send();
client.onreadystatechange = function() {
  if(client.readyState == 4 && client.status == 200) {
    document.getElementById("htNews").innerHTML = client.responseText;
  }
}
</script>

</div>
</div>
<div id="bgLeft" class="bg box"></div>
<div id="bgRight" class="bg box"></div>
</div>
<div id="middleContainer" class="box">

<div id="ctitle">Python</div>
<ul class="comicNav">
<li><a href="/1/">|&lt;</a></li>
<li><a rel="prev" href="/352/" accesskey="p">&lt; Prev</a></li>
<li><a href="//c.xkcd.com/random/comic/">Random</a></li>
<li><a rel="next" href="/354/" accesskey="n">Next &gt;</a></li>
<li><a href="/">&gt;|</a></li>
</ul>
<div id="comic">
<img src="//imgs.xkcd.com/comics/python.png" title="I wrote 20 short programs in Python yesterday.  It was wonderful.  Perl, I&#39;m leaving you." alt="Python" />
</div>
<ul class="comicNav">
<li><a href="/1/">|&lt;</a></li>
<li><a rel="prev" href="/352/" accesskey="p">&lt; Prev</a></li>
<li><a href="//c.xkcd.com/random/comic/">Random</a></li>
<li><a rel="next" href="/354/" accesskey="n">Next &gt;</a></li>
<li><a href="/">&gt;|</a></li>
</ul>
<br />
Permanent link to this comic: https://xkcd.com/353/<br />
Image URL (for hotlinking/embedding): https://imgs.xkcd.com/comics/python.png
<div id="transcript" style="display: none">[[ Guy 1 is talking to Guy 2, who is floating in the sky ]]
Guy 1: You&#39;re flying! How?
Guy 2: Python!
Guy 2: I learned it last night! Everything is so simple!
Guy 2: Hello world is just &#39;print &quot;Hello, World!&quot; &#39;
Guy 1: I dunno... Dynamic typing? Whitespace?
Guy 2: Come join us! Programming is fun again! It&#39;s a whole new world up here!
Guy 1: But how are you flying?
Guy 2: I just typed &#39;import antigravity&#39;
Guy 1: That&#39;s it?
Guy 2: ...I also sampled everything in the medicine cabinet for comparison.
Guy 2: But i think this is the python.
{{ I wrote 20 short programs in Python yesterday.  It was wonderful.  Perl, I&#39;m leaving you. }}</div>
</div>
<div id="bottom" class="box">
<img src="//imgs.xkcd.com/s/a899e84.jpg" width="520" height="100" alt="Selected Comics" usemap="#comicmap"/>
<map id="comicmap" name="comicmap">
<area shape="rect" coords="0,0,100,100" href="/150/" alt="Grownups"/>
<area shape="rect" coords="104,0,204,100" href="/730/" alt="Circuit Diagram"/>
<area shape="rect" coords="208,0,308,100" href="/162/" alt="Angular Momentum"/>
<area shape="rect" coords="312,0,412,100" href="/688/" alt="Self-Description"/>
<area shape="rect" coords="416,0,520,100" href="/556/" alt="Alternative Energy Revolution"/>
</map>
<br />
<a href="//xkcd.com/1732/"><img border=0 src="//imgs.xkcd.com/s/temperature.png" width="520" height="100" alt="Earth temperature timeline"></a>
<br />
<div>
<!--
Search comic titles and transcripts:
<script type="text/javascript" src="//www.google.com/jsapi"></script>
<script type="text/javascript">google.load('search', '1');google.setOnLoadCallback(function() {google.search.CustomSearchControl.attachAutoCompletion('012652707207066138651:zudjtuwe28q',document.getElementById('q'),'cse-search-box');});</script>
<form action="//www.google.com/cse" id="cse-search-box">
<div>
<input type="hidden" name="cx" value="012652707207066138651:zudjtuwe28q"/>
<input type="hidden" name="ie" value="UTF-8"/>
<input type="text" name="q" id="q" size="31"/>
<input type="submit" name="sa" value="Search"/>
</div>
</form>
<script type="text/javascript" src="//www.google.com/cse/brand?form=cse-search-box&amp;lang=en"></script>
-->
<a href="/rss.xml">RSS Feed</a> - <a href="/atom.xml">Atom Feed</a> - <a href="/newsletter/">Email</a>
</div>
<br />
<div id="comicLinks">
Comics I enjoy:<br/>
        <a href="http://threewordphrase.com/">Three Word Phrase</a>,
        <a href="http://www.smbc-comics.com/">SMBC</a>,
        <a href="http://www.qwantz.com">Dinosaur Comics</a>,
        <a href="http://oglaf.com/">Oglaf</a> (nsfw),
        <a href="http://www.asofterworld.com">A Softer World</a>,
        <a href="http://buttersafe.com/">Buttersafe</a>,
        <a href="http://pbfcomics.com/">Perry Bible Fellowship</a>,
        <a href="http://questionablecontent.net/">Questionable Content</a>,
        <a href="http://www.buttercupfestival.com/">Buttercup Festival</a>,
        <a href="http://www.mspaintadventures.com/?s=6&p=001901">Homestuck</a>,
	<a href="http://www.jspowerhour.com/">Junior Scientist Power Hour</a>
</div>
<br />
<div id="comicLinks">
Other things:<br/>
        <a href="https://medium.com/civic-tech-thoughts-from-joshdata/so-you-want-to-reform-democracy-7f3b1ef10597">Tips on technology and government</a>,<br /> 
        <a href="https://www.nytimes.com/interactive/2017/climate/what-is-climate-change.html">Climate FAQ</a>,
	<a href="https://twitter.com/KHayhoe">Katharine Hayhoe</a>
</div>
<br />
<center>
<div id="footnote" style="width:70%">xkcd.com is best viewed with Netscape Navigator 4.0 or below on a Pentium 3&plusmn;1 emulated in Javascript on an Apple IIGS<br />at a screen resolution of 1024x1. Please enable your ad blockers, disable high-heat drying, and remove your device<br />from Airplane Mode and set it to Boat Mode. For security reasons, please leave caps lock on while browsing.</div>
</center>
<div id="licenseText">
<p>
This work is licensed under a
<a href="http://creativecommons.org/licenses/by-nc/2.5/">Creative Commons Attribution-NonCommercial 2.5 License</a>.
</p><p>
This means you're free to copy and share these comics (but not to sell them). <a rel="license" href="/license.html">More details</a>.</p>
</div>
</div>
</body>
<!-- Layout by Ian Clasbey, davean, and chromakode -->
</html>


In [8]:
#URL of an image
r= requests.get('https://imgs.xkcd.com/comics/python.png')
#To download the image
with open('comic.png','wb') as f: #wb is used for image which means writebyte
    f.write(r.content)
In [9]:
#To check status code
print(r.status_code) #200 is for success, 300 is for redirects, 400 is for client error, 500 are for server error
200
In [11]:
#for anything less than 400 status code it will print True or only for client and server error it will print False
print(r.ok)
True
In [12]:
#will print the response headers
print(r.headers)
{'Server': 'nginx', 'Content-Type': 'image/png', 'Last-Modified': 'Mon, 01 Feb 2010 13:07:49 GMT', 'ETag': '"4b66d225-162d3"', 'Expires': 'Tue, 16 Jul 2019 16:25:42 GMT', 'Cache-Control': 'max-age=300', 'Content-Length': '90835', 'Accept-Ranges': 'bytes', 'Date': 'Tue, 16 Jul 2019 18:22:17 GMT', 'Via': '1.1 varnish', 'Age': '193', 'Connection': 'keep-alive', 'X-Served-By': 'cache-bom18224-BOM', 'X-Cache': 'HIT', 'X-Cache-Hits': '1', 'X-Timer': 'S1563301337.142568,VS0,VE3'}
In [13]:
#dictionary of parameters to pass in url
payload={ 'page':2, 'count':25}
#httpbin is a testing website develop by developer of request module
r= requests.get('https://httpbin.org/get', params=payload)  
print(r.text)
{
  "args": {
    "count": "25", 
    "page": "2"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.21.0"
  }, 
  "origin": "103.85.125.190, 103.85.125.190", 
  "url": "https://httpbin.org/get?page=2&count=25"
}

In [14]:
print(r.url) #request module set the url correctly so it's always better to add parameter this way than manually.
https://httpbin.org/get?page=2&count=25
In [15]:
#To make post request
payload={ 'username':'corey', 'password':'text'}
r= requests.post('https://httpbin.org/post', data=payload)  
print(r.text)
#we are getting json as reponse so it's better to use json method for that
{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "password": "text", 
    "username": "corey"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Content-Length": "28", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.21.0"
  }, 
  "json": null, 
  "origin": "103.85.125.190, 103.85.125.190", 
  "url": "https://httpbin.org/post"
}

In [18]:
#It will give the json response in form of dictionary
r_dict=r.json()
print(r_dict)
{'args': {}, 'data': '', 'files': {}, 'form': {'password': 'text', 'username': 'corey'}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Content-Length': '28', 'Content-Type': 'application/x-www-form-urlencoded', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.21.0'}, 'json': None, 'origin': '103.85.125.190, 103.85.125.190', 'url': 'https://httpbin.org/post'}
In [19]:
#To print only the form
print(r_dict['form'])
{'password': 'text', 'username': 'corey'}
In [20]:
#Basic Authentication

r =requests.get('https://httpbin.org/basic-auth/kushagra/12345678',auth=('kushagra',12345678))
#auth is tuple of id,password
print(r.text)
{
  "authenticated": true, 
  "user": "kushagra"
}

In [21]:
print(r)  #status code 200 means success
<Response [200]>
In [22]:
#with wrong password
r =requests.get('https://httpbin.org/basic-auth/kushagra/12345678',auth=('kushagra',123456))
print(r) #401 means authentication error
<Response [401]>
In [32]:
#URl that will give response after 1sec
r =requests.get('https://httpbin.org/delay/1',timeout=3) #timeout means it will wait for max 3 sec to get reponse
print(r)  #as response came in lesser time than timeout hence success 
<Response [200]>
In [38]:
#URl that will give response after 5secs
try:
    r =requests.get('https://httpbin.org/delay/5',timeout=3)
except Exception as e:
    print(e)
else:
    print(r)
#Exception is raised as timeout is lesser than response time.
HTTPSConnectionPool(host='httpbin.org', port=443): Read timed out. (read timeout=3)